package com.bosssoft.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bosssoft.baseEnity.SelectItem;
import com.bosssoft.baseParam.BaseParam;
import com.bosssoft.entity.po.FibSealPO;
import com.bosssoft.entity.po.FibVoucherDetailsPO;
import com.bosssoft.entity.vo.FibVoucherDetailsVO;
import com.bosssoft.exception.DaoException;
import com.bosssoft.fegin.OssFeginService;
import com.bosssoft.page.PageResult;
import com.bosssoft.response.Result;
import com.bosssoft.service.FibVoucherDetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Aria
 */
//真实凭证不能删不能改
//只能查和添加
@RequestMapping("/voucherDetails")
@RestController
@Api(tags = "凭证盖章(凭证详细)" +
        "")
public class FibVoucherDetailsController {

    @Autowired
    private FibVoucherDetailsService fibVoucherDetailsService;

    @Autowired
    private OssFeginService ossFeginService;

    @ApiOperation(value = "盖章")
    @PostMapping("/stamp")
    public Result stampTest(@Validated({BaseParam.add.class}) @RequestBody FibVoucherDetailsVO fibVoucherDetailsVO) {

        String url = null;
        System.out.println(11111);
        System.out.println(fibVoucherDetailsVO);
        try {
            url = fibVoucherDetailsService.stamp(fibVoucherDetailsVO);
            if (url == null) {
                return Result.error();
            }
            return Result.ok().data("url", url);
        } catch (DaoException daoException) {
            if (daoException.getMessage() != null) {
                return Result.error(daoException.getCode(), daoException.getMessage());
            }
        }
        return Result.error();
    }

    @ApiOperation(value = "凭证下拉框")
    @PostMapping("/getSealSelectItem")
    public Result getSealSelectItem() {
        try {
            QueryWrapper<FibVoucherDetailsPO> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByAsc("voucher_id");
            List<FibVoucherDetailsPO> fibVoucherDetailsPOS = fibVoucherDetailsService.getBaseMapper().selectList(queryWrapper);
            SelectItem[] selectItems = new SelectItem[fibVoucherDetailsPOS.size()];
            for (int i = 0; i < fibVoucherDetailsPOS.size(); i++) {
                selectItems[i] = new SelectItem("凭证编号: " + fibVoucherDetailsPOS.get(i).getVoucherId(), fibVoucherDetailsPOS.get(i).getVoucherId().toString());
            }
            return Result.ok().data("map", selectItems);
        } catch (DaoException e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("/voucherPage")
    public Result voucherPage(@RequestParam(defaultValue = "1") Integer pageNo,
                              @RequestParam(defaultValue = "10") Integer pageSize, FibVoucherDetailsVO fibVoucherDetailsVO) {
        try {
            PageResult<FibVoucherDetailsPO> page = fibVoucherDetailsService.voucherPage(pageNo, pageSize, fibVoucherDetailsVO);
            return Result.ok().data("page", page);
        } catch (DaoException e) {
            if (e.getMessage() != null) {
                return Result.error(e.getCode(), e.getMessage());
            }
            return Result.error();
        }
    }

    @ApiOperation(value = "根据id查找凭证")
    @PostMapping("/selectVoucherById")
    public Result selectVoucherById(FibVoucherDetailsVO fibVoucherDetailsVO) {
        try {
            FibVoucherDetailsPO fibVoucherDetailsPO = fibVoucherDetailsService.selectVoucherById(fibVoucherDetailsVO);
            Result.ok().data("data", fibVoucherDetailsPO);
        } catch (DaoException e) {
            if (e.getMessage() != null) {
                return Result.error(e.getCode(), e.getMessage());
            }
            return Result.error();
        }
        return Result.ok();
    }

    @ApiOperation(value = "查找一条凭证信息_根据凭证编码与预算申请编号")
    @PostMapping("/selectByBgtIdAndCode")
    public Result selectByBgtIdAndCode(Integer voucherCode, Long bgtId) {
        try {
            FibVoucherDetailsPO fibVoucherDetailsPO = fibVoucherDetailsService.selectByBgtIdAndCode(voucherCode, bgtId);
            Result.ok().data("data", fibVoucherDetailsPO);
        } catch (DaoException e) {
            if (e.getMessage() != null) {
                return Result.error(e.getCode(), e.getMessage());
            }
            return Result.error();
        }
        return Result.ok();
    }

}
